package com.psq.business.login.router

import android.app.Activity
import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.psq.base.user.manager.UserManager
import com.psq.business.login.ui.activity.LoginActivity
import com.psq.core.common.model.ApiResponse
import com.psq.core.common.provider.ILoginProvider
import com.psq.core.common.provider.ROUTER_LOGIN_PROVIDER
import com.psq.core.common.utils.navigation
import com.psq.business.login.network.api.LoginService
import com.psq.business.login.repository.LoginRepository
import com.psq.core.common.di.RetrofitModule
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.stateIn

@Route(path = ROUTER_LOGIN_PROVIDER)
class LoginProvider : ILoginProvider {

    private val repository by lazy { LoginRepository(RetrofitModule.create(LoginService::class.java)) }

    override fun navLogin() = navigation(ROUTER_PATH_LOGIN)
    override fun navForgetPwd() {

    }

    override fun navResetPwd() {
    }


    override fun isLoginActivity(activity: Activity): Boolean {
        return activity is LoginActivity
    }

    override fun activityIsLoginModule(activity: Activity): Boolean {
        return activity is LoginActivity
    }

    override suspend fun logout(): ApiResponse<Any> {
        val coroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
        val userPreferences =
            UserManager.getInstance().userPreferencesFlow.stateIn(coroutineScope).value
        return repository.logout(mutableMapOf())
    }

    override fun init(context: Context?) {
    }
}